﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;

/// <summary>
/// Loads Jquery 
/// </summary>
[Extension("Jquery", "1.0.0.0", "<a href=\"http://www.jquery.com/\">Jquery.com</a>")]
public class Jquery
{
    #region Private members
    private const string _jqueryRoot = "js/";
    private const string _scriptFile = "jquery-1.3.2.min.js";

    static protected ExtensionSettings _settings = null;

    #endregion


	public Jquery()
	{
        // subscribe for post serving event
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
        BlogEngine.Core.Page.Serving +=
            new EventHandler<ServingEventArgs>(Post_Serving); //For Pages
    }
    /// <summary>
    /// An event that handles ServingEventArgs
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Post_Serving(object sender, ServingEventArgs e)
    {
        if (e.Location == ServingLocation.PostList || e.Location == ServingLocation.SinglePage || e.Location == ServingLocation.SinglePost)
        {
            AddJsToTheHeader();
        }
    }

    /// <summary>
    /// Inject JavaScript file into the header of the post
    /// </summary>
    private void AddJsToTheHeader()
    {
        // get a page handler
        System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
        bool added = false;

        // check if script already added to the page header
        foreach (Control control in page.Header.Controls)
        {
            if (control.GetType() == typeof(HtmlGenericControl))
            {
                HtmlGenericControl htmlGenericControl = (HtmlGenericControl)control;
                if (htmlGenericControl.Attributes["src"] != null)
                {
                    if (htmlGenericControl.Attributes["src"].Contains(_scriptFile))
                    {
                        added = true;
                    }
                }
            }
        }

        if (!added)
        {
            HtmlGenericControl js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.Attributes.Add("language", "javascript");
            js.Attributes.Add("src", GetJqueryRoot() + _scriptFile);
            
            HtmlGenericControl jsLoad = new HtmlGenericControl("script");
            jsLoad.InnerHtml = "$j = jQuery.noConflict();";
            jsLoad.Attributes.Add("type", "text/javascript");
            jsLoad.Attributes.Add("language", "javascript");

            
            page.Header.Controls.Add(js);
            page.Header.Controls.Add(jsLoad);
        }
    }

    /// <summary>
    /// Virtual path to jquery script folder.
    /// </summary>
    /// <returns>Path to the jquery script folder.</returns>
    private string GetJqueryRoot()
    {
        string virtualPath = HttpContext.Current.Request.Path;
        string audioRoot = virtualPath.Substring(0, virtualPath.LastIndexOf("/") + 1) + _jqueryRoot;
        return audioRoot;
    }
}


